package fi.bluepill.server.model;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import java.util.HashSet;
import java.util.Set;

@Entity
public abstract class RateableEntity extends CommentableEntity {
    private Double averageRating = 0.0;
    private Double ratingsSum = 0.0;
    private Integer ratingsCount = 0;

    @OneToMany(fetch = FetchType.LAZY)
    private Set<Rating> ratings = new HashSet<Rating>();

    public Set<Rating> getRatings() {
        return ratings;
    }

    public Double getAverageRating() {
        return averageRating;
    }


    public Integer getRatingsCount() {
        return ratingsCount;
    }

    public void addRating(Rating newRating) {
        ratings.add(newRating);

        ratingsCount++;
        ratingsSum += newRating.getRating();

        this.averageRating = ratingsSum / ratingsCount;
    }
}
